
## Snakes on a Payload
## Copyright (C) 2011 CORE Security Technologies
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##
## Authors:
## 2011/09/11 - Pedro Varangot (pvarangot@)
## 2011/09/11 - Fernando Russ (fruss@)


import StringIO
import struct
from relocation import Relocation



class R_ARM_BaseRelocation(Relocation): # (S + A) | T

    def apply(self, section, output):       
        THUMB = 0 # default, no THUMB
        
        relocation_value = ( self.value + self.addend ) | THUMB        
        section[self.offset:self.offset+4:] = struct.pack("<I", relocation_value)
        
    
@Relocation.register
class R_ARM_RELATIVE(R_ARM_BaseRelocation):
    NAME = "R_ARM_RELATIVE" # B + A
    
    def apply(self, section, output):
        pass
        
    def parseSymbol(self, values):
        return 0, 0, ""                
        ##
        ## read relocs informacion from section
        ##            
        #for name, section in self.elf.sections.items():
            #section_limit = section.addr+section.size
            #if self.offset >= section.addr and self.offset<section_limit:
                                
                #offset = self.offset - section.addr # convert from RVA -> LA
                                
                #v = struct.unpack("<I",str(section.content[offset:offset+4]))[0]
                
                #print "%s %s %x" % (self.elf.objfile, self.NAME, v )
                
                #return 0, v, ""                
               
@Relocation.register
class R_ARM_ABS32(R_ARM_BaseRelocation):
    NAME = "R_ARM_ABS32" # (S + A) | T
                
@Relocation.register
class R_ARM_JUMP_SLOT(R_ARM_BaseRelocation):
    NAME = "R_ARM_JUMP_SLOT" # (S + A) | T
    
@Relocation.register
class R_ARM_GLOB_DAT(R_ARM_BaseRelocation):
    NAME = "R_ARM_GLOB_DAT" # (S + A) | T

